package com.flashphoner.api
{
	import com.flashphoner.api.data.Call;
	import com.flashphoner.api.data.ErrorCodes;
	import com.flashphoner.api.data.ModelLocator;
	import com.flashphoner.api.data.PhoneConfig;
	import com.flashphoner.api.interfaces.APINotify;
	import com.flashphoner.api.management.PhoneSpeaker;
	import com.flashphoner.api.management.SoundControl;
	import com.flashphoner.api.management.VideoControl;
	
	import flash.events.TimerEvent;
	import flash.external.ExternalInterface;
	import flash.media.Camera;
	import flash.media.Microphone;
	import flash.net.Responder;
	import flash.utils.Timer;

	[Bindable]
	public class Flash_API
	{
		internal var phoneServerProxy:PhoneServerProxy;
		internal var currentCall:Call = null;	
		
		public var apiNotify:APINotify;		

		public var modelLocator:ModelLocator = new ModelLocator();
		public var state:String = State.out;
		public var timeOfCall:int = 0;
		private var timer:Timer;
		public function Flash_API()
		{
			phoneServerProxy = new PhoneServerProxy(new Responder(result,fault),this);
			apiNotify = new APINotifyImpl(this);
		}
		
		public function getCall():Call{
			return currentCall;
		}
		
		public function getPhoneSpeaker():PhoneSpeaker{
			return this.phoneServerProxy.phoneSpeaker;
		}
		
		public function login(username:String,password:String):int{
			ExternalInterface.call("notifyRegisterRequired",PhoneConfig.REGISTER_REQUIRED);
			return phoneServerProxy.login(username,password);							
		}
		
		public function call(callee:String, visibleName:String, isVideoCall:Boolean = false):int{
			if (visibleName != null){
				visibleName.replace("\"","");
				visibleName.replace("\'","");
			}
			state = State.call;
			phoneServerProxy.call(visibleName,callee,isVideoCall);
			return 0;
		}
		
		public function hangup():void{
			if (currentCall!=null){
				phoneServerProxy.unpublish(currentCall);	
				phoneServerProxy.hangup(currentCall);										
			}	
		}
		
		public function answer(isVideoCall:Boolean = false):void{
			phoneServerProxy.answer(currentCall,isVideoCall);			
		}
		public function isSendVideo(flag:Boolean):void{
			phoneServerProxy.isSendVideo(flag);		
		} 
				
		public function sendDTMF(dtmf:String):void{
			phoneServerProxy.sendDtmf(currentCall,dtmf);			
		}		
		public  function logoff():void{
			phoneServerProxy.disconnect();
		}
		
		private function fault(data : Object) : void
		{
		}		
		
		private function result(_call : Object) : void
		{
			var call:Call = new Call();
			call.id = _call.id;
			call.state = _call.state;
			call.incoming=false;							
			currentCall=call;
		}		
		
		public function startTimer():void{
			if (timer!=null){
				timer.removeEventListener(TimerEvent.TIMER,timerHandler);
				timer.stop();
				timer = null;
			}
			timeOfCall = 0;
			timer = new Timer(999);
			timer.addEventListener(TimerEvent.TIMER,timerHandler);					
			timer.start();			
		}
		
		private function timerHandler(timeEvent:TimerEvent):void{
			timeOfCall++;
		}
		
		public function stopTimer(){
			timeOfCall = 0;
			if (timer != null){
				timer.removeEventListener(TimerEvent.TIMER,timerHandler);
				timer.stop();
				timer = null;
			}
		}
		
	}
}